/** */
package com.ttbj.core.dao.redis.impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ttbj.core.dao.redis.IRedisBaseDao;
import com.ttbj.core.dao.redis.IRedisHashBaseDao;
import redis.clients.jedis.ShardedJedis;

/**
 * @Description: redisHashBaseDao 实现类
 * @Author: Daniel
 * @CreateDate: 2015-4-27 下午05:49:13
 * @Version: V1.0
 */

@Repository("redisHashBaseDao")
public class RedisHashBaseDaoImpl implements IRedisHashBaseDao {

    private static final Logger logger = LoggerFactory.getLogger(RedisHashBaseDaoImpl.class);
    @Autowired
    private IRedisBaseDao redisBaseDao;

    /**
     * @author : 刘旋
     * @date : 2018/1/31 - 11:15
     * @description : 将哈希表 key 中的字段 field 的值设为 value 。
     */
    @Override
    public Long hSet(String key, String field, String value) {
        Long result = null;
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field) || StringUtils.isBlank(value)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hset(key, field, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    /**
     * @author : 刘旋
     * @date : 2018/1/31 - 11:15
     * @description : 同时将多个 field-value (域-值)对设置到哈希表 key 中。
     */
    @Override
    public String hmSet(final String key, Map<String, String> map) {
        String result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hmset(key, map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Long hSetNX(String key, String field, String value) {
        Long result = null;
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field) || StringUtils.isBlank(value)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hsetnx(key, field, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public List<String> hVals(String key) {
        List<String> result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hvals(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    /**
     * @author : 刘旋
     * @date : 2018/1/31 - 11:15
     * @description : 获取存储在哈希表中指定字段的值。
     */
    @Override
    public String hGet(final String key, final String field) {
        String result = null;
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hget(key, field);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    /**
     * @author : 刘旋
     * @date : 2018/1/31 - 11:15
     * @description : 获取在哈希表中指定 key 的所有字段和值
     */
    @Override
    public Map<String, String> hGetAll(final String key) {
        Map<String, String> result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hgetAll(key);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Boolean hExists(String key, String field) {
        Boolean result= null;
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hexists(key, field);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Long hDel(String key, String... fields) {
        Long result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hdel(key, fields);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Long hIncrBy(String key, String field, Long increment) {
        Long result = null;
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hincrBy(key, field, increment);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Double hIncrByFloat(String key, String field, Double increment) {
        Double result = null;
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hincrByFloat(key, field, increment);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Set<String> hKeys(String key) {
        Set<String> result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hkeys(key);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public Long hLen(String key) {
        Long result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hlen(key);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }

    @Override
    public List<String> hmGet(String key, String... fields) {
        List<String> result = null;
        if (StringUtils.isBlank(key)) {
            return result;
        }
        ShardedJedis shardedJedis = redisBaseDao.getResource();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.hmget(key, fields);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            redisBaseDao.returnResource(shardedJedis, broken);
        }
        return result;
    }
}
